<?php
/**
 * 短信，彩信发送封装服务类
 * @author kim007
 *
 */
class SenderService extends Service {
	public $smsClient;
	public $mmsClient;
	public $config;
	public $config_mms;
	public $config_sms;
	public function __construct() {
		$this->init();
	}
	/** 发送短信 
	 * @param array $mobiles		手机号, 如 array('159xxxxxxxx'),如果需要多个手机号群发,如 array('159xxxxxxxx','159xxxxxxx2') 
	 * @param string $content		短信内容
	 * @return array 标准格式 参见emayParseResult
	 */
	public function sendSms($mobiles,$content){
		Log::record('发送中心记录：SenderService->sendSms '.date('Y-m-d H:i:s'),Log::K_ORDER,Log::FILE);
		
		$result = $this->smsClient->sendSMS($mobiles,$content);
		
		Log::record('发送中心记录：SenderService->sendSms  result= '.parseAry2Str($result),Log::K_ORDER,Log::FILE);
		Log::save();
		return $result;
	}
	
	
	/** 发送彩信 
	 * @param array $userNumbers	手机号, 如 array('159xxxxxxxx'),如果需要多个手机号群发,如 array('159xxxxxxxx','159xxxxxxx2') 
	 * @param string $MMSContent	彩信内容
	 * @param string $title 		彩信标题
	 * @return array 标准格式 参见emayParseResult
	 * */
	public function sendMms($title,$mobiles,$MMSContent){
		Log::record('发送中心记录：SenderService->sendMms '.date('Y-m-d H:i:s'),Log::K_ORDER,Log::FILE);
		
		$result = $this->mmsClient->sendMms($title,$mobiles,$MMSContent);
		
		Log::record('发送中心记录：SenderService->sendMms  result= '.parseAry2Str($result),Log::K_ORDER,Log::FILE);
		Log::save();
		return $result;
	}
	
	/**
	 * SMS登陆
	 @return array 标准格式 参见emayParseResult
	 */
	public function login4Sms(){
		Log::record('发送中心记录：login4Sms->starting--------------------',Log::K_ORDER,Log::FILE);
		$result = $this->smsClient->login();
		Log::record('发送中心记录：login4Sms->登录结果'.parseAry2Str($result),Log::K_ORDER,Log::FILE);
		Log::record('发送中心记录：login4Sms->ended-----------------------',Log::K_ORDER,Log::FILE);
		Log::save();
		return $result;
	}
	
	/**
	 * SMS注销
	 * @return array 标准格式 参见emayParseResult
	 */
	public function logout4Sms(){
		Log::record('发送中心记录：logout4Sms->starting--------------------',Log::K_ORDER,Log::FILE);
		$result = $this->smsClient->logout();
		Log::record('发送中心记录：logout4Sms->登录结果'.parseAry2Str($result),Log::K_ORDER,Log::FILE);
		Log::record('发送中心记录：logout4Sms->ended-----------------------',Log::K_ORDER,Log::FILE);
		Log::save();
		return $result;
	}
	
	/**
	 * 获取SMS余额
	 * @return array 标准格式 参见emayParseResult
	 */
	public function getBalance4Sms(){
		Log::record('发送中心记录：getBalance4Sms->starting--------------------',Log::K_ORDER,Log::FILE);
		$result = $this->smsClient->getBalance();
		Log::record('发送中心记录：getBalance4Sms->余额：'.parseAry2Str($result),Log::K_ORDER,Log::FILE);
		Log::record('发送中心记录：getBalance4Sms->ended-----------------------',Log::K_ORDER,Log::FILE);
		Log::save();
		return $result;
	}
	
	/**
	 * 获取MMS余额
	 * @return array 标准格式 参见emayParseResult
	 */
	public function getBalance4Mms(){
		Log::record('发送中心记录：getBalance4Mms->starting--------------------',Log::K_ORDER,Log::FILE);
		$result = $this->mmsClient->getBalance();
		Log::record('发送中心记录：getBalance4Mms->余额：'.parseAry2Str($result),Log::K_ORDER,Log::FILE);
		Log::record('发送中心记录：getBalance4Mms->ended-----------------------',Log::K_ORDER,Log::FILE);
		Log::save();
		return $result;
	}
	
	private function initSmsConfig($config_){
		$config['gwUrl']			=$config_['gwUrl'];
		$config['serialNumber']		=$config_['serialNumber'];
		$config['password']			=$config_['password'];
		$config['sessionKey']		=$config_['sessionKey'];

		if(intval($config_['connectTimeOut'])>0)
			$config['connectTimeOut']	=$config_['connectTimeOut'];
		else 
			$config['connectTimeOut']	=2;
			
		if(intval($config_['readTimeOut'])>0)
			$config['readTimeOut']	=$config_['readTimeOut'];
		else 
			$config['readTimeOut']	=10;
		
		if(!empty($config_['proxyhost']))
			$config['proxyhost']	=$config_['proxyhost'];
		else 
			$config['proxyhost']	=false;
		
		if(!empty($config_['proxyport']))
			$config['proxyport']	=$config_['proxyport'];
		else 
			$config['proxyport']	=false;
			
		if(!empty($config_['proxyusername']))
			$config['proxyusername']	=$config_['proxyusername'];
		else 
			$config['proxyusername']	=false;
			
		if(!empty($config_['proxypassword']))
			$config['proxypassword']	=$config_['proxypassword'];
		else 
			$config['proxypassword']	=false;
		
		$this->config_sms	= $config;
	}
	
	
	private function initMmsConfig($config_){
		$config['MMS_gwUrl']	= 	$config_['MMS_gwUrl'];
		$config['MMS_userName']	= 	$config_['MMS_userName'];
		$config['MMS_password']	= 	$config_['MMS_password'];
		$config['MMS_sendType']	=	$config_['MMS_sendType'];
		
		$this->config_mms	= $config;
	}
	
	//服务初始化
	public function init() {
		$provider = 'emay';
		$smsConfig = model('Xdata')->lget('sms');
		$config = unserialize($smsConfig[$provider]);
		define('SCRIPT_ROOT',  SMS_LIB_PATH.'/'.$provider.'/');
		require_once SCRIPT_ROOT.'include/SmsClient.php';
		require_once SCRIPT_ROOT.'include/MmsClient.php';
		require_once SCRIPT_ROOT.'include/'.$provider.'ParseResult.php';
		$this->initSmsConfig($config);
		$this->initMmsConfig($config);
		$this->config=$config;
		$this->initSmsClient($this->config_sms);
		$this->initMmsClient($this->config_mms);
	}
	
	
	public function initSmsClient($config){
		Log::record('发送中心记录：initSmsClient= '.parseAry2Str($config),Log::K_ORDER,Log::FILE);
		$client = new SmsClient($config['gwUrl'],$config['serialNumber'],
								$config['password'],$config['sessionKey'],
								$config['proxyhost'],$config['proxyport'],
								$config['proxyusername'],$config['proxypassword'],
								$config['connectTimeOut'],$config['readTimeOut']);
		$this->smsClient=$client;
		$this->smsClient->setOutgoingEncoding("UTF-8");
		//$this->smsClient->sessionKey='';
		if(true||empty($this->smsClient->sessionKey)){
			
		}else{
			//Log::record('发送中心记录：开始--'.date('Y-m-d H:i:s'),Log::K_ORDER,Log::FILE);
		}
		Log::save();
	}
	
	
	public function initMmsClient($config){
		Log::record('发送中心记录：initMmsClient= '.parseAry2Str($config),Log::K_ORDER,Log::FILE);
		$this->mmsClient=new MmsClient($config['MMS_gwUrl'],$config['MMS_userName'],$config['MMS_password'],$config['MMS_sendType']);
		Log::save();
	}

	
	//运行服务，系统服务自动运行
	public function run() {
	}
}
?>